package com.neusoft.elmboot.controller;

import java.util.List;

import com.neusoft.elmboot.dto.CartDTO;
import com.neusoft.elmboot.po.User;
import com.neusoft.elmboot.service.ex.*;
import com.neusoft.elmboot.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.neusoft.elmboot.po.Cart;
import com.neusoft.elmboot.service.CartService;

@RestController
@RequestMapping("/carts")
public class CartController {
	public static final int OK = 200;
	public static final int DATA_NOT_FOUND = 204;
	public static final int ERROR = 500;

	@Autowired
	private CartService cartService;
	
	@GetMapping
	public JsonResult<List<Cart>> listCart(Cart cart) {
		JsonResult<List<Cart>> result = new JsonResult<List<Cart>>();
		try {
			List<Cart> data = cartService.listCart(cart);
			result = new JsonResult<List<Cart>>(OK, data);
			result.setMessage("获取购物车列表成功");
			return result;
		} catch (DataNotFoundException e) {
			// 处理异常
			result.setState(DATA_NOT_FOUND);
			result.setMessage(e.getMessage());
			return result;
		}
	}

	//首次添加食物到购物车
	@PostMapping
	public JsonResult<Cart> saveCart(@RequestBody Cart cart) throws Exception{
		JsonResult<Cart> result = new JsonResult<Cart>();
		if (cart.getQuantity() != null) {
			result.setState(ERROR);
			result.setMessage("首次向购物车添加一个新的商品时，不应该提供商品的数量！");
			return result;
		}
		try{
			int data = cartService.saveCart(cart);
			result = new JsonResult<Cart>(OK,cart);
			result.setMessage("添加商品成功");
			return result;
		}
		 catch (DataNotFoundException e) {
			// 处理异常
			result.setState(DATA_NOT_FOUND);
			result.setMessage(e.getMessage());
			return result;
		} catch (Exception e) {
			// 处理异常
			result.setState(ERROR);
			result.setMessage(e.getMessage());
			return result;
		}
	}

	//在购物车中增加或者减少食物数量
	@PutMapping
	public JsonResult<Cart> updateCart(@RequestBody Cart cart) throws Exception{
		JsonResult<Cart> result = new JsonResult<Cart>();
		//if (cart.getQuantity() != 1 || cart.getQuantity() != -1) {
		//	result.setState(ERROR);
		//	result.setMessage("前端传入数量不是1或-1，更新购物车商品数量失败!");
		//	return result;
		//}
		try {
			int data = cartService.updateCart(cart);
			result = new JsonResult<Cart>(OK, cart);
			result.setMessage("更新购物车商品数量成功");
			return result;
		}catch(UpdateException e){
			result.setState(ERROR);
			result.setMessage(e.getMessage());
			return result;
		}
	}
	
	@DeleteMapping
	public JsonResult<Cart> removeCart(@RequestBody Cart cart) throws Exception{
		JsonResult<Cart> result = new JsonResult<Cart>();
		try {
			int data = cartService.removeCart(cart);
			result = new JsonResult<Cart>(OK, cart);
			result.setMessage("删除购物车商品成功");
			return result;
		}catch(DeleteException e){
			result.setState(ERROR);
			result.setMessage(e.getMessage());
			return result;
		}
	}

	@DeleteMapping("/clear")
	public JsonResult<CartDTO> clearCart(@RequestBody CartDTO cartDTO) throws Exception{
		JsonResult<CartDTO> result = new JsonResult<CartDTO>();
		try {
			int data = cartService.clearCart(cartDTO);
			result = new JsonResult<CartDTO>(OK, cartDTO);
			result.setMessage("清空购物车成功");
			return result;
		}catch (DeleteException e){
			result.setState(ERROR);
			result.setMessage(e.getMessage());
			return result;
		}
	}
}
